<template>
  <view class="affirm">
	  <view class="consignee" @click="editAddress()" v-if="address">
		  <view class="img">
		  	<image src="../../../static/img/address.png" mode=""></image>
		  </view>
		  <view class="address">
		  	<view>{{ address.contactUser }} <text class="right">{{ address.contactPhone }}</text></view>
		  	<view class="site">收货地址：{{ address.province }} {{ address.city }} {{ address.region }} {{ address.addrDetail }}</view>
		  </view>
		  <image src="../../../static/img/rightarrow.png" mode="" class="rightarrow"></image>
	  </view>
	  <view class="consignee" @click="editAddress()" v-else>
	  	<p class="add">
	  		<image src="../../../static/img/add1.png" mode=""></image>
	  		添加地址
	  	</p>
	  	<image src="/static/img/consignee_bg.png" />
	  </view>
	<view class="divider"></view>
	<view class="list">
		<view class="img">
			<image :src="this.detail.image" mode="aspectFit"></image>
		</view>
		<view class="right">
			<view class="title">{{this.detail.goodsName}}</view>
			<view class="date">x{{this.initData.num}}</view>
			<view class="jifen">
				<text>{{this.detail.pointAmount}}</text>积分
			</view>			
		</view>
	</view>
	<view class="divider"></view>
	<view class="detail">
		<view >
			<text>所用积分</text>{{this.initData.usepoint}}积分  
		</view>
		<view>
			<text>兑换时间</text>{{this.detail.createdTime}}
		</view>
	</view>
	<view class="state" style="display: none;">已兑换</view>
	<view class="button" @click="submit">立即兑换</view>
  </view>
</template>
<script>
import { uniIcons } from '@dcloudio/uni-ui';
import integral from '../../../servers/integral/integral.js';
import affirm from '../../../servers/order/affirm.js';
export default {
	components: { uniIcons },
	data() {
		return {
			initData:'',
			// count:'',
			goodsPointId:'',
			address: [],
			// usepoint:'',
			detail:'',
			isDoRefresh:false
		};
	},
	// mounted(){
	// 	this.getaddress();
	// },
	// onShow() {
	// 	this.getaddress();
	// },
	onLoad(option) {
		// this.goodsPointId=option.goodsPointId;
		// this.count=option.num
		if(option.detail){
			this.initData=JSON.parse(option.detail) 
			this.detail=JSON.parse(this.initData.detail) 
			this.goodsPointId=this.detail.id;
			// this.count=this.initData.num
			// this.usepoint=this.initData.usepoint
			console.log(this.initData)
			
		}
		
		this.getaddress();
	},
	onUnload(){
	    uni.$off('checkedAddress') 
	},
	onShow() {
		
		
		
		
		
		this.getaddress();
	},
	methods: {
		getaddress() {
			// uni.showLoading({
			// 	title: '加载中',
			// 	mask: true
			// });
			if(this.isDoRefresh){
				this.receive();
			}else{
				affirm
					.deliverAddress()
					.then(res => {
						if(res.data.length>0){
							for (var i = 0; i < res.data.length; i++) {
								console.log(res.data[i].isDefault.value)
								if (res.data[i].isDefault.value == 1) {
									
									this.address = res.data[i];
								}
							}
						}else{
							this.address=[]
						}
						
						
						console.log(this.address )
					})
					.catch(err => {
						console.log(new Error('请检查网络后重试'));
					});
			}
			
				// uni.hideLoading();
		},
		editAddress() {
			this.commonality.route_baling('/pages/shop/my/address','address','order')
			this.receive();
		},
		receive() {
			var _this = this;
			uni.$once('checkedAddress', function(data) {
				console.log('监听到事件来自 checked ，携带参数为：');
				console.log(data);
				_this.address = data;
			});
		},
		submit(){
			var params={
				count:this.initData.num,
				goodsPointId:this.goodsPointId,
				orderExpressId:this.address.id
			}
			// console.log(this.goodsPointId)
			integral
				.exchange(params)
				.then(res => {
					if(res.code==200){
						uni.navigateTo({
							url:'./success'
						})
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
				.catch(err => {
					console.log(new Error('请检查网络后重试'));
				});
			
		}
	}
};
</script>
<style lang="less" scoped>
.affirm {
	background: #fff;
	height: 100%;
	padding: 1rpx 0;
	.consignee {
		position: relative;
		padding: 20rpx;
		justify-content: space-between;
		font-size: 28rpx;
		line-height: 69rpx;
		border-radius: 10rpx;
		overflow: hidden;
		display: flex;
		align-items: center;
		.img{
			width: 80rpx;
			height: 80rpx;
			margin-right: 20rpx;
		}
		.address{
			flex: 1;
			padding-right: 20rpx;
		}
		image {
			
			width: 80rpx;
			height: 80rpx;
		}
		.rightarrow{
			width: 18rpx;
			height: 34rpx;
		}
		.site {
			position: relative;
			line-height: 50rpx;
			color: #999;
			.uni-arrowright {
				position: absolute;
				top: 2rpx;
				right: 0rpx;
			}
		}
		
	}
	.divider{
		height: 26rpx;
		background-color: #f6f6f6;
	}
	.list{
		padding:30rpx 20rpx;
		display: flex;
		.img{
			border:1px solid #e1e1e1;
			border-radius: 12rpx;		
			width: 194rpx;
			height: 182rpx;
			margin-right: 20rpx;
			image{
				width: 100%;
				height: 100%;
			}
		}
		.right{
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			flex: auto;
			color: #000;
			.title{
				font-size: 28rpx;				
			}
			.date{
				font-size: 30rpx;				
			}
			.jifen{
				font-size: 24rpx;
				color: #999;
				text{
					font-size: 36rpx;
					color: #f70225;
					margin-right: 20rpx;
					vertical-align: sub;
				}
			}
		
		
		}
		
	}
	.detail{
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #999;
		view{
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #e1e1e1;
			padding: 30rpx 0;
			text{
				font-size: 26rpx;
				color: #333;
				
			}
		}
		
	}
	.state{
		font-size: 30rpx;
		color: #fff;
		background-color: #c5c5c5;
		width: 335rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		margin:100rpx auto 0;
		border-radius: 50rpx;
	}
	.button {
		line-height: 90rpx;
		background: linear-gradient(80deg, #d2158a, #fc6395);
		text-align: center;
		color: #fff;
		border-radius: 45rpx;
		font-size: 32rpx;
		box-shadow: 3rpx 0 20rpx rgba(206, 205, 204, 0.35);
		margin: 60rpx 20rpx;
	}
	
}
</style>
